/**
 * create by young (young2f@outlook.com) on 2020/9/16
 */


'use strict';
import React from 'react';
import {Button, Form, notification, Select} from 'antd';

import {FormField, FormLayout, ModalFooter} from 'antd-extra';
import FormBase from '../../../components/Base/FormBase';
import {connect} from "dva";
import moment from 'moment';
import 'moment/locale/zh-cn';
import UploadPic from "../../../components/UploadPic";

moment.locale('zh-cn');

@Form.create()
@connect(({
            global,
            dict
          }) => ({...dict, ...global}))
export default class ModalCreateOrEditPersonnel extends FormBase {
  constructor(props) {
    super(props);
    const {record = {}} = this.props;
    this.state = {...record};
  }

  componentDidMount() {
    this.props.dispatch({
      type: 'dict/findMany',
      payload: {type: 'division'}
    });
  }

  renderFoot = () => {
    const {foot, value, record, callback, modalRef, form} = this.props;

    if (foot) {
      return foot(value, modalRef);
    } else {
      return (
        <ModalFooter>
          <Button type="primary" onClick={() => {
            form.validateFields((err, values) => {

              if (err) {
                const {code} = err;
                const {errors} = code;
                errors.forEach(item => {
                  notification.error(item);
                });
                return;
              }
              values.recordId = record ? record.recordId : 'RID' + moment().format('YYYYMMDDHHmmss');
              values.fileList = this.state.fileList;
              values.division = this.state.division;
              values.xlFileList = this.state.xlFileList;
              values.htFileList = this.state.htFileList;
              callback(values);
              modalRef.close();
            });

          }}>确定</Button>
          <Button onClick={() => {
            modalRef.close();
          }}>取消</Button>
        </ModalFooter>
      );
    }
  };

  callback = () => {
    this.componentDidMount();
  };

  render() {
    const {form, model, divisionDict} = this.props;
    const { division, countNumber = 0,xlFileList=[],htFileList=[]} = this.state;
    const {record = {count: countNumber}} = this.props;
    return (
      <div>
        <FormLayout form={form} cols={1}>
          <FormField
            label="所属公司"
            name="division"
            key={'division'}
            required={true}
            initialValue={division}
          >
            <Select required allowClear placeholder="请选择" style={{width: '100%'}}
                    onChange={value => {
                      this.state.division = value;
                    }}
            >
              {this.getDivision(divisionDict)}
            </Select>
          </FormField>
          {this.getDicFields(model, record, this.callback)}
        </FormLayout>

        <UploadPic title={'上传学历证书'} fileList={xlFileList} callback={f => {
          this.state.xlFileList = f.fileList;
        }}></UploadPic>
        <UploadPic title={'上传合同'} fileList={htFileList} callback={f => {
          this.state.htFileList = f.fileList;
        }}></UploadPic>
        {this.renderFoot()}
      </div>
    );
  }
}
